﻿using Help;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLLA
{
    public partial class Order_Int_T : BaseBLL<Model.Order_Int_T>, IBLL.IOrder_Int_TBLL
    {
        ObjectSerialize os = new ObjectSerialize();
        DataResult result = new DataResult();
        IBLL.IBLLSession bl = DI.SpringHelper.GetObject<IBLL.IBLLSession>("BLLSession");
        /// <summary>
        /// 获取商户积分
        /// </summary>
        /// <param name="MerID">商户ID</param>
        /// <param name="YZM">验证码</param>
        /// <param name="RETID">数据格式（1：JSON；2：XML）</param>
        /// <returns></returns>
        public string GetOrder_Int(string MerID, string YZM, int RETID)
        {
            #region 匹配验证码
            if (YZM != Help.ALL_DataV.YZM)
            {
                result.BS = "-88";
                result.Msg = "验证码错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion
            #region 业务层
            List<Model.View_Order_Int> ls = new List<Model.View_Order_Int>();
            try
            {
                ls = base.DBSession.IView_Order_IntDAL.GetListBy(u => u.MerID == MerID & u.IntState == 1);
            }
            catch (Exception)
            {
                result.BS = "-1";
                result.Msg = "查询不到数据！";
                return os.StringOfObject(result, RETID);
            }
            result.BS = "1";
            result.Msg = "查询成功！";
            result.rows = ls;
            return os.StringOfObject(result, RETID);
            #endregion
        }

        /// <summary>
        /// 更新商户积分管理信息（当删除积分时要把对应的用户积分全部清空）
        /// </summary>
        /// <param name="OperID">操作员ID</param>
        /// <param name="json">更新信息</param>
        /// <param name="tag">操作标签（1：新增；2：修改；3：删除）</param>
        /// <param name="YZM">验证码</param>
        /// <param name="RETID">数据格式（1：JSON；2：XML）</param>
        /// <returns></returns>
        public string SetOrder_Int(int OperID, string json, int tag, string YZM, int RETID)
        {
            #region 匹配验证码
            if (YZM != Help.ALL_DataV.YZM)
            {
                result.BS = "-88";
                result.Msg = "验证码错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion
            #region 执行操作
            try
            {
                Model.Order_Int_T info = ParseClass<Model.Order_Int_T>.StrDeserialize(json, RETID);
                string T = "";
                int ret = -1;
                // 根据操作标签Tag判断是什么操作
                switch (tag)
                {
                    case 1:
                        T = "新增";
                        List<Model.Order_Int_T> l = base.GetListBy(u => u.MerID == info.MerID);
                        if (l.Count > 0)
                        {
                            info.IntState = 1;
                            Model.Order_Int_T tmp22 = l[0];
                            info.IntID = tmp22.IntID;
                            Help.FuZhi<Model.Order_Int_T>.FZ(ref tmp22, info);
                            ret = base.Modify(tmp22);  
                        }
                        else
                        {
                            info.IntID = GetSysKey.GetKey("IntKey");
                            info.IntState = 1;
                            ret = base.Add(info);
                        }
                        break;
                    case 2:
                        T = "修改";
                        info.IntState = 1;
                        Model.Order_Int_T tmp = base.GetListBy(u => u.IntID == info.IntID).First();
                        Help.FuZhi<Model.Order_Int_T>.FZ(ref tmp, info);
                        ret = base.Modify(tmp);  
                        break;
                    case 3:
                        T = "删除";
                        List<Model.Order_Int_T> list_tmp = base.GetListBy(u => u.IntID == info.IntID);
                        if (list_tmp != null && list_tmp.Count > 0)
                        {
                            Model.Order_Int_T tmp1 = list_tmp.First();
                            tmp1.IntState = 0;
                            ret = base.Modify(tmp1);
                            if (ret > 0)
                            {
                                // 删除成功后，清空所用用户对应的积分
                                List<Model.Order_UserInt_T> list_userint = bl.IOrder_UserInt_TBLL.GetListBy(u => u.IntID == info.IntID);
                                if (list_userint != null && list_userint.Count > 0)
                                {
                                    foreach (Model.Order_UserInt_T item in list_userint)
                                    {
                                        item.IntNowNub = 0;
                                        bl.IOrder_UserInt_TBLL.Modify(item);
                                    }
                                }
                            }
                        }
                        break;
                }

                string isSH = "商户";
                if (info.MerID == "0")
                {
                    isSH = "平台";
                }
               
                if (ret > 0)
                {
                    // 将操作记录到日志表中
                    Model.Order_OperationFlow_T Log = new Model.Order_OperationFlow_T();
                    Log.OperationTitle = "更新"+isSH+"积分管理信息";
                    Log.OperationText = T + isSH+"积分管理信息成功";
                    Log.OperationType = "1F0006";
                    Log.OperationRes = "成功";
                    Log.OperationUser = OperID;
                    string str_log = os.StringOfObject(Log, RETID);
                    bool isSuccess = new Order_OperationFlow_T().AddOrder_OperationFlow(str_log, RETID);
                    if (!isSuccess)
                    {
                        WriteLog.WriteW("新增系统操作流水失败");
                    }
                    result.BS = "1";
                    result.Msg = "更新成功";
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "更新失败";
                }
            }
            catch (Exception ex)
            {
                WriteLog.WriteW("更新商户的订单状态和备注信息失败：" + ex.Message);
                result.BS = "-1";
                result.Msg = "更新失败";
            }
            #endregion
            return os.StringOfObject(result, RETID);
        }

        /// <summary>
        /// 根据操作员查询商户积分信息
        /// </summary>
        /// <param name="OperID">操作员ID</param>
        /// <param name="YZM">验证码</param>
        /// <param name="RETID">数据格式（1：JSON；2：XML）</param>
        /// <returns></returns>
        public string GetOrder_MerInt(int OperID, string YZM, int RETID)
        {
            #region 匹配验证码
            if (YZM != Help.ALL_DataV.YZM)
            {
                result.BS = "-88";
                result.Msg = "验证码错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion
            #region 业务层
            List<Model.View_Order_Int> ls = new List<Model.View_Order_Int>();
            try
            {
                // 根据操作员ID查询其商户ID
                List<Model.Order_Oper_T> ls_mer = bl.IOrder_Oper_TBLL.GetListBy(u => u.OperID == OperID);

                if (ls_mer != null && ls_mer.Count > 0)
                {
                    Model.Order_Oper_T mer = ls_mer.First();
                    string[] mers = mer.MerID.Split(',');
                    foreach (string merid in mers)
                    {
                        List<Model.View_Order_Int> ls_tmp = base.DBSession.IView_Order_IntDAL.GetListBy(u => u.MerID == merid & u.IntState == 1);
                        if (ls_tmp != null && ls_tmp.Count > 0)
                        {
                            Model.View_Order_Int order_int = ls_tmp.First();
                            ls.Add(order_int);
                        }
                    }
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "查询不到操作员对应的商户！";
                    return os.StringOfObject(result, RETID);
                }
            }
            catch (Exception)
            {
                result.BS = "-1";
                result.Msg = "查询失败！";
                return os.StringOfObject(result, RETID);
            }
            result.BS = "1";
            result.Msg = "查询成功！";
            result.rows = ls;
            return os.StringOfObject(result, RETID);
            #endregion
        }
    }
}
